/**
 * 新增修改完成调用 modalFormOk方法 编辑弹框组件ref定义为modalForm
 * 高级查询按钮调用 superQuery方法  高级查询组件ref定义为superQueryModal
 * data中url定义 list为查询列表  delete为删除单条记录  deleteBatch为批量删除
 */
import { wyv3del, getActionV3 } from '@/http/axios.js'
export const JeecgListMixin = {
    data() {
        return {
            /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
            queryParam: {},
            /* 数据源 */
            tableData: [],
            /* 分页参数 */
            ipagination: {
                current: 1,
                pageSize: 10,
                pageSizeOptions: ['10', '50', '100'],
                layout: 'total, sizes, prev, pager, next, jumper',
                showSizeChanger: true,
                total: 100
            },
            /* table加载状态 */
            loading: false,
            /* 表头样式 */
            headerCellStyle:{backgroundColor:`#fafafa`,color:`#000`}
        }
    },
    created() {
        this.loadData();
    },
    methods: {
        loadData(arg) {
            if (!this.url.list) {
                this.$message.error("请设置url.list属性!")
                return
            }
            var params = this.getQueryParams(); //查询条件
            this.loading = true;
            getActionV3(this.url.list, params).then((res) => {
                if (res.code === 200) {
                    this.tableData = res.data.records;
                    if (res.data.total) {
                        this.ipagination.total = res.data.total;
                    } else {
                        this.ipagination.total = 0;
                    }
                } else {
                    this.$message.warning(res.message)
                }
            }).finally(() => {
                this.loading = false
            })
        },
        getQueryParams() {
            let param = {}
            param.pageNo = this.ipagination.current;
            param.pageSize = this.ipagination.pageSize;
            return param;
        },

        onSelectChange(selectedRowKeys, selectionRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectionRows = selectionRows;
        },
        searchQuery() {
            this.loadData(1);
        },
        superQuery() {
            this.$refs.superQueryModal.show();
        },
        handleEdit: function(record) {
            console.log(record);
            this.$refs.modalForm.edit(record);
            this.$refs.modalForm.title = "编辑";
            this.$refs.modalForm.disableSubmit = false;
        },
        modalFormOk() {
            // 新增/修改 成功时，重载列表
            this.loadData();
            //清空列表选中
            // this.onClearSelected()
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.ipagination.pageSize = val
            this.loadData();
        },
        handleCurrentChange(val) {
            this.ipagination.pageNo = val
            console.log(`当前页: ${val}`);
            this.loadData();
        },
        handleDelete: function(id) {
            if (!this.url.delete) {
                this.$message.error("请设置url.delete属性!")
                return
            }
            let that = this;
            wyv3del(that.url.delete, {
                id: id
            }).then((res) => {
                if (res.code === 200) {
                    that.$emit('ok')
                    that.loadData();
                      //重新计算分页问题
                    that.reCalculatePage(1)
                    that.$message.success(res.message)

                } else {
                    that.$message.error(res.message)
                }
            });
        },
        handleAdd: function() {
            this.$refs.modalForm.add();
            this.$refs.modalForm.title = "新增";
            this.$refs.modalForm.disableSubmit = false;
        },
        reCalculatePage(count){
          //总数量-count
          let total=this.ipagination.total-count;
          //获取删除后的分页数
          let currentIndex=Math.ceil(total/this.ipagination.pageSize);
          //删除后的分页数<所在当前页
          if(currentIndex<this.ipagination.current){
            this.ipagination.current=currentIndex;
          }
          console.log('currentIndex',currentIndex)
        },
    }

}
